<?php
require_once 'widgets/TSkinnableWidget.inc';

/**
 * 
 * @author kras
 * @property TCMSCategoryModel $CategoryModel
 */

class TNewsRibbonWidget extends TSkinnableWidget {
	protected $_int_length_ = 6;
	protected $_conf_category_ = null;
	protected $_conf_rss_page_ = null;

/**
 * @var TCMSCategoryModel
 */	
	protected $_ioc_category_model_;

/**
 * @var IKeyValueRepository
 */	
	protected $_ioc_settings_repo_;
	
	private $_list_ = null;
	private $_category_ = null;
	
	protected function checkSource($src){
		return $src instanceof TCMSPageModel;
	}
	
	private function _get_list(){
		if (is_null($this->_list_)){
			$c = $this->Category;
			if (!$c)
				if ($this->SettingsRepo)
					$c = $this->SettingsRepo->GetValue("NewsCategory");
			
			if ($c){
				$cat = $this->CategoryModel->GetList(new TListOptions(array(new TFilterOption("Slug", TConditionType::C_EQUAL, $c))));
				
				foreach ($cat as $c1)
					$this->_category_ = $c1;
				
				if ($this->_category_)
					$this->_list_ = $this->Source->GetList(new TListOptions(array(new TFilterOption('Categories', TConditionType::C_IN, array($this->_category_->ItemId())),new TFilterOption('IsPublished', TConditionType::C_EQUAL, true)),array(new TSortingOption("Published",TSortingType::SORT_DESC)),$this->Length));
			}
		}
		if (is_null($this->_list_))
			$this->_list_ = new TArrayIterator(array());
	}
	
	public function __get($nm){
		switch ($nm){
			case 'List':{
				$this->_get_list();
				return $this->_list_;
			}break;
			case 'ItemsCategory':{
				$this->_get_list();
				return $this->_category_;
			}break;
			default:return parent::__get($nm);break;
		}
	}
	
	public function defaultOutput(IThemeEngine $engine){
			?>
			<div id="<?php echo $this->Name(); ?>" class="news-ribbon">
				<div class="heading">
				<?php 
				__i($this->ItemsCategory); 
				if ($this->RssPage){
				?>
				<a class="rss-link" href="<?php echo htmlentities($this->response->Service()->Url($this->RssPage)); ?>"><div></div></a>
				<?php
				}
				?>
				</div>
				<div class="ribbon">
				<?php
					foreach ($this->List as $p){
						?>
						<div class="issue">
						<div class="issue-date"><?php __d($p->Published); ?></div>
						<?php echo $p->Snippet; ?>
						<a class="readmore-link" href="<?php echo htmlentities($this->Response()->Service()->ItemNavigationUrl($p)); ?>">
						<?php __i("Read more"); ?>
						</a>
						</div>
						<?php
					} 
				?>
				</div>
			</div>
			<?php 
	}
}